﻿using System;
namespace LeRenard.ScriptWars.Model.Scripting.OperationParameterTypeValueConverters
{
    /// <summary>
    /// Handles conversion from <see cref="OperationParameterType.Boolean"/> to <see cref="Boolean"/>.
    /// </summary>
    public sealed class BooleanParameterTypeValueConverter : OperationParameterTypeValueConverter<bool>
    {
        /// <summary>
        /// Creates a new <see cref="BooleanParamaterTypeValueConverter"/> object.
        /// </summary>
        public BooleanParameterTypeValueConverter() : base(OperationParameterType.Boolean) { }

        /// <summary>
        /// Converts the raw script value to a .Net type value.
        /// </summary>
        /// <param name="parameterValue">The value to be converted.</param>        
        public override bool ConvertFromScriptValue(string parameterValue)
        {
            ThrowIf.NullOrEmpty(() => parameterValue);

            if (parameterValue.Equals("true"))
                return true;

            if (parameterValue.Equals("false"))
                return false;

            throw new ArgumentException("Can not convert '{0}' to a bool value.".FormatWith(parameterValue), "parameterValue");
        }

        /// <summary>
        /// Converts a .Net type value to the raw script value.
        /// </summary>
        /// <param name="parameterValue">the value to be converted.</param>        
        public override string ConvertToScriptValue(bool parameterValue)
        {
            if (parameterValue)
                return "true";

            return "false";
        }
    }
}
